﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Net;
using System.Net;
using ReflectionStudio.Core.Events;
using System.Net.Sockets; 
using System.Text; 
using System.Threading;
namespace WPFExcelReport
{

    public class SocClient
    {
        public Socket soc = null;

        private int _port;
        public int Port
        {
            get { return _port; }
            private set { _port = value; }
        }

        private IPAddress _ip;
        public IPAddress Ip
        {
            get { return _ip; }
            private set { _ip = value; }
        }

        public SocClient(IPAddress ip, int nPort)
        {
            this.Port = nPort;
            this.Ip = ip;
        }

        /// <summary>
        /// 连接Socket服务端
        /// </summary>
        /// <returns>是否连接成功</returns>
        public bool Connect()
        {
            soc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            try
            {
                soc.Connect(new IPEndPoint(Ip, Port));
                return true;
            }
            catch
            {
                return false;
            }
        }

        public void DisConnect()
        {
            if (null != soc)
            {
                soc.Shutdown(SocketShutdown.Both);
                soc.Close();
            }
        }

        public string ReceiveData()
        {
            string strData = string.Empty;
            if (null != soc && soc.Connected)
            {
                byte[] bytes = new byte[1024];
                int nLength = soc.Receive(bytes);
                for (int i = 0; i < nLength; i++)
                {
                    strData += bytes[i].ToString("X2"); //十六进制
                    //strData += Encoding.ASCII.GetString(bytes);//其它字符串
                }
                strData = "";
                if (bytes[0] == 2)
                {
                    try
                    {
                        string sReceiveData = System.Text.Encoding.ASCII.GetString(bytes);
                        //MessageBox.Show(sReceiveData);
                        //解析命令,将读到的字符解析成数字,注意高低位的转换
                        for (int i = 1; i < 1024; i += 4)
                        {
                            string sLow = sReceiveData.Substring(i, 2);
                            string sHigh = sReceiveData.Substring(i + 2, 2);
                            //int res = Convert.ToInt32(sHigh)+ Convert.ToInt32(sLow);
                            int res = Convert.ToInt32(sHigh, 16) + Convert.ToInt32(sLow, 16);

                            strData += res.ToString() + ",";
                        }
                    }
                    catch (Exception ex)
                    {

                    }

                }
            } 
            return strData;
        } 

        public bool SendComData(string strData)
        {
            //byte[] bytes = strToToHexByte(strData);
            byte[] bytes = System.Text.Encoding.ASCII.GetBytes(strData);
            try
            {
                soc.Send(bytes);
                return true;
            }
            catch
            {
                soc.Shutdown(SocketShutdown.Both);
                soc.Close();
            }
            return false;
        }

        public class StateObject
        {
            // Client socket. 
            public Socket workSocket = null;
            // Size of receive buffer. 
            public const int BufferSize = 256;
            // Receive buffer. 
            public byte[] buffer = new byte[BufferSize];
            // Received data string. 
            public StringBuilder sb = new StringBuilder();
        } 
    }
    
}
